package net.yambal.air.trackManager.GPX
{
	
	import com.adobe.protocols.dict.events.ErrorEvent;
	import com.adobe.utils.DateUtil;
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	
	import mx.collections.ArrayCollection;

	public class GPX
	{
		
		private var _xml:XML;
        
        [Bindable]
        public var name:String;
        
        [Bindable]
        public var time:Date;
		
		[Bindable]
		public var GpxVersion:String;
		
		public var creator:String;
        
        [Bindable]
        public var WayPoints:ArrayCollection;   // GPX.WPT のリスト
        
        [Bindable]
        public var Tracks:ArrayCollection;      // GPX.TRK のリスト
        
        [Bindable]
        public var Rutes:ArrayCollection;      // GPX.RTE のリスト

		public var SouthLat:Number = 999;
		public var WestLon:Number = 999;
		public var NorthLat:Number = -999;
		public var EastLon:Number = -999;

		public function GPX(xml:XML = null){
			this.xml = xml;
		}
		
		public function set xml(xml:XML):void{
			this._xml = xml;
			this._parse();
		}
        
        private function _parse():void{
            this._xml.ignoreWhitespace = true;
            var ns:Namespace = this._xml.namespace();
            
            this.name = this._xml.ns::name;
            this.GpxVersion = this._xml.@version;
            this.creator = this._xml.@creator;
            try{
                this.time = DateUtil.parseW3CDTF(this._xml.ns::time);
            }catch(e:Error){
                trace('gpx.time 無し');
            }
            
            // Rte
            try{
                this._parseRute();
            }catch(e:Error){
                trace('70');
            }
            
            // TRK
            try{
                this._parseTrk();
            }catch(e:Error){
                trace('77');
            }
            
            // Wpt
            this._parseWpt();
            
            // GPX.BOUNDS
            var bounds:XMLList = this._xml.ns::bounds;
            if(!bounds || bounds.length() != 0){
                this.SouthLat　= bounds[0].@minlat;
                this.WestLon = bounds[0].@minlon;
                this.NorthLat = bounds[0].@maxlat;
                this.EastLon = bounds[0].@maxlon;
            }else{
                trace('GPX.BOUNDS 無し');
            }
        }
        
        /**
        * GPX.WPT
        **/
        private function _parseWpt():void{
            this.WayPoints = new ArrayCollection();
            
            this._xml.ignoreWhitespace = true;
            var ns:Namespace = this._xml.namespace();
            
            var wpts:XMLList = this._xml.ns::wpt;
            if(!wpts || wpts.length() == 0){
                return;
            }
            
            for(var i:Number = 0; i < wpts.length(); i++){
                var wpt:WayPoint = new WayPoint(wpts[i] as XML, WayPoint.POINT_TYPE_WPT);
                this.WayPoints.addItem(wpt);
            }
            
            if(wpt.lat < this.SouthLat){
                this.SouthLat = wpt.lat;
            }
            if(wpt.lat > this.NorthLat){
                this.NorthLat = wpt.lat;
            }
            if(wpt.lon < this.WestLon){
                this.WestLon = wpt.lon;
            }
            if(wpt.lon > this.EastLon){
                this.EastLon = wpt.lon;
            }
        }
        
        /**
        * GPX.TRK
        **/
        private function _parseTrk():void{
            this.Tracks = new ArrayCollection();
            
            this._xml.ignoreWhitespace = true;
            var ns:Namespace = this._xml.namespace();
            
            var trks:XMLList = this._xml.ns::trk;
            if(!trks || trks.length() == 0){
                return;
            }

            for(var i:Number = 0; i < trks.length(); i++){
                var track:TrackArrayCollection = new TrackArrayCollection(trks[i] as XML);
                this.Tracks.addItem(track);
            }
            
            if(track.SouthLat < this.SouthLat){
                this.SouthLat = track.SouthLat;
            }
            if(track.NorthLat > this.NorthLat){
                this.NorthLat = track.NorthLat;
            }
            if(track.WestLon < this.WestLon){
                this.WestLon = track.WestLon;
            }
            if(track.EastLon > this.EastLon){
                this.EastLon = track.EastLon;
            }
        }
        
        /**
        * GPX.RTE
        **/
        private function _parseRute():void{
            this.Rutes = new ArrayCollection();
            
            this._xml.ignoreWhitespace = true;
            var ns:Namespace = this._xml.namespace();
            var rtes:XMLList = this._xml.ns::rte;

            if(!rtes || rtes.length() == 0){
                return;
            }
            
            for(var i:Number = 0; i < rtes.length(); i++){
                var rute:WayPointArrayCollection = new WayPointArrayCollection(rtes[i], WayPointArrayCollection.LIST_TYPE_RTE);
                this.Rutes.addItem(rute);
                
                if(rute.SouthLat < this.SouthLat){
                    this.SouthLat = rute.SouthLat;
                }
                if(rute.NorthLat > this.NorthLat){
                    this.NorthLat = rute.NorthLat;
                }
                if(rute.WestLon < this.WestLon){
                    this.WestLon = rute.WestLon;
                }
                if(rute.EastLon > this.EastLon){
                    this.EastLon = rute.EastLon;
                }
            }
        }

		public function get bounds():LatLngBounds{
			var southWest:LatLng = new LatLng(this.SouthLat, this.WestLon);
			var northEast:LatLng = new LatLng(this.NorthLat, this.EastLon);
			return new LatLngBounds(southWest,northEast);
		}
		
		public function get center():LatLng{
			return new LatLng((this.SouthLat + this.NorthLat) / 2, (this.WestLon + this.EastLon) / 2);
		}
	}
}